const app = getApp();
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    all_cat: [],
    courseSelect: 0,
    selected_tags: [],
    gradeNameSelect: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;
    var coach_current_course_id = wx.getStorageSync('coach_current_course_id');
    var coach_current_grade_id = wx.getStorageSync('coach_current_grade_id');
    //获取
    app.sendRequest({
      url: app.api.public.grade_course_student,
      success: function(res) {
        if (res.data != undefined) {
          let all_cat = util.getCourses(res.data.list);
          let current_grade = 0;
          if (all_cat[0].grade_id) {
            current_grade = all_cat[0].grade_id;
          }
          let class_list = [];
          if (coach_current_grade_id) {
            all_cat.map(subject => {
              if (subject.grade_id == coach_current_grade_id) {
                class_list = subject.course_list;
              }
            });
          } else {
            class_list = all_cat[0].course_list;
          }
          page.setData({
            all_cat: all_cat,
            current_grade: current_grade,
            class_list: class_list,
            coach_current_course_id: coach_current_course_id,
            coach_current_grade_id: coach_current_grade_id
          });

          wx.setStorageSync('grade_course_student', res.data);
        }
      }
    });
  },
  frmClick(e) {
    app.saveFormId(e.detail.formId);
  },
  changeData: function() {
    this.onLoad();
  },
  selectTag: function(e) {
    var page = this;
    var selectVal = e.currentTarget.dataset.select;
    var courseSelect = page.data.courseSelect;
    var class_list = page.data.class_list;
    var all_cat = page.data.all_cat;

    if (selectVal == -1) {
      wx.setStorageSync('coach_current_course_id', '');
      if (!wx.getStorageSync('coach_gradeNameSelect')) {
        wx.setStorageSync('coach_current_grade_id', all_cat[0].grade_id);
        wx.setStorageSync('coach_gradeNameSelect', all_cat[0].grade_name);
      }
    } else {
      class_list.map(subject => {
        if (subject.course_id == selectVal) {
          if (subject.checked) {
            // subject.checked = false;
            // courseSelect = 0;
          } else {
            subject.checked = true;
            courseSelect = selectVal;
            wx.setStorageSync('coach_current_course_id', courseSelect);
            // wx.setStorageSync('coach_current_grade_id', page.data.current_grade);
            if (!wx.getStorageSync('coach_gradeNameSelect')) {
              wx.setStorageSync('coach_current_grade_id', all_cat[0].grade_id);
              wx.setStorageSync('coach_gradeNameSelect', all_cat[0].grade_name);
            }
          }
        } else {
          subject.checked = false;
        }
      });
    }

    page.setData({
      class_list: class_list,
      courseSelect: courseSelect
    });

    wx.navigateTo({
      url:
        '/pages/coach_center/coach_center?grade_id=' +
        wx.getStorageSync('coach_current_grade_id') +
        '&course_id=' +
        wx.getStorageSync('coach_current_course_id')
    });
  },

  //切换分类
  topclass: function(e) {
    let page = this;
    let o_id = e.currentTarget.dataset.id;
    let gradeNameSelect = e.currentTarget.dataset.name;
    let class_list = [];
    let all_cat = page.data.all_cat;

    wx.setStorageSync('coach_current_grade_id', o_id);
    wx.setStorageSync('coach_gradeNameSelect', gradeNameSelect);

    all_cat.map(cat => {
      if (cat.grade_id == o_id) {
        if (cat.course_list) {
          class_list = cat.course_list;
        } else {
          class_list = [];
        }
      }
    });

    //设置当前样式
    page.setData({
      current_grade: o_id,
      coach_current_grade_id: o_id,
      class_list: class_list,
      all_cat: all_cat,
      gradeNameSelect: gradeNameSelect
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var page = this;
    page.onLoad();
    // 设置content高度
    wx.getSystemInfo({
      success: function(res) {
        page.setData({
          // second部分高度 = 窗口可使用高度
          content_height: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
